package com.itgm.jiayun_web_project.controller;

import ch.qos.logback.core.util.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itgm.jiayun_web_project.pojo.Admin;
import com.itgm.jiayun_web_project.pojo.LoginInfo;
import com.itgm.jiayun_web_project.pojo.PageResult;
import com.itgm.jiayun_web_project.pojo.Result;
import com.itgm.jiayun_web_project.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;


import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/admin")
@Tag(name = "管理员接口", description = "管理员接口")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Operation(summary = "管理员登录")
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin, HttpSession session) {
        log.info("管理员登录");
        LoginInfo loginInfo = adminService.login(admin);

        if (loginInfo == null) {
            log.info("用户不存在");

            return Result.error("用户不存在");
        }

        log.info("登录成功");
        //保存用户名到session中
        session.setAttribute("username", loginInfo.getUsername());
        session.setAttribute("userId", loginInfo.getId());
        return Result.success(loginInfo);
    }

    @Operation(summary = "管理员退出登录")
    @GetMapping("/logout")
    public Result logout(HttpSession session) {
        log.info("管理员退出登录");
        session.removeAttribute("username"); //移除session中的用户名
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<PageResult<Admin>> page(@RequestParam Integer page,
                                   @RequestParam Integer pageSize,
                                   @RequestParam(required = false) String name) {
        log.info("分页查询");
        return adminService.page(page, pageSize, name);
    }

    //新增管理员
    @PostMapping
    @Operation(summary = "新增管理员")
    public Result add(@RequestBody Admin admin) throws NoSuchAlgorithmException {
        log.info("新增管理员");
        String password = admin.getPassword();
        //密码进行md5加密
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        admin.setPassword(s);

        adminService.save(admin);
        return Result.success();
    }

    /**
     * 启用/禁用管理员
     * @param admin
     * @return
     */
    @PutMapping("/changeStatus")
    @Operation(summary = "启用/禁用管理员")
    public Result changeStatus(@RequestBody Admin admin){
        Admin admin1 = adminService.getById(admin.getId());
        if (admin1.getStatus()==0){
            admin1.setStatus(1);
            return adminService.updateById(admin1)?Result.success("启用成功"):Result.error("启用失败");
        }else {
            admin1.setStatus(0);
            return adminService.updateById(admin1)?Result.success("禁用成功"):Result.error("禁用失败");
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询管理员")
    public Result<Admin> getById(@PathVariable("id") Long id) {
        log.info("根据id查询管理员");
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }

    @PutMapping
    @Operation(summary = "修改管理员")
    public Result update(@RequestBody Admin admin) throws NoSuchAlgorithmException {
        log.info("修改管理员");
        String password = admin.getPassword();
        //密码进行md5加密
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        admin.setPassword(s);
        boolean b = adminService.updateById(admin);
        return b?Result.success():Result.error("修改失败");


    }


}
